﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class ComenziWizFrm : FrmRezumat
    {

        private String GestiuneSt0c = String.Empty;

        private Int32 IDArticol = -1;
        private Int64 IDPartener = -1;
        public Int32 IndexSelect = -1;
        public Int32 IndexIDDocDetaliuDest = -1;
        public Int32 IndexIDDocDetaliuSursa = -1;
        public Int32 IndexIDArticol = -1;
        public Int32 IndexCantitate = -1;
        public Int32 IndexCantitateOld = -1;
        public Int32 IndexUM = -1;
        public Int32 IndexValoareValuta = -1;
        public Int32 IndexValoareLei = -1;
        private Int16 IDTipDocumentSursa = -1;
        private Decimal CantitateIesita = 0;

        public Boolean WizardComenzi = false;

        private Int64 IDDocDetaliuDest = -1;
        public ComenziWizFrm(ModuleClass mc, Int32 idarticol, Int64 iDDocDetaliuDest,Int64 idpart,Int16 idTipDoc,String tipLink = "ST",Decimal cantitateIesita=0)
            : base(mc)
        {
            InitializeComponent();
            IDArticol = idarticol;
            IndexSelect = SelectColumn.Index;
            IndexIDDocDetaliuDest = IDDocDetaliuDestColumn.Index;
            IndexIDDocDetaliuSursa = IDDocumentDetaliuColumn.Index;
            IndexIDArticol = ArticoleColumn.Index;
            IndexCantitate = CantitateReceptColumn.Index;
            IndexUM = IDUnitateMasuraColumn.Index;
            IndexValoareValuta = ColumnValoareValuta.Index;
            IndexValoareLei = ColumnValoareLei.Index;
            IDTipDocumentSursa = idTipDoc;
            IndexCantitateOld = CantitateIesitaColumn.Index;
            GestiuneSt0c = tipLink;
            CantitateIesita = cantitateIesita;

            IDDocDetaliuDest = iDDocDetaliuDest;
            IDPartener = idpart;

            DeLaDTP.Format = DateTimePickerFormat.Custom;
            DeLaDTP.CustomFormat = "dd.MM.yyyy";

            PanaLaDTP.Format = DateTimePickerFormat.Custom;
            PanaLaDTP.CustomFormat = "dd.MM.yyyy";

            this.dataGridViewTextBoxColumn11.DisplayMember = "Denumire";
            this.dataGridViewTextBoxColumn11.ValueMember = "CodValuta";

            if (GestiuneSt0c=="ST")
            {
                CantitateReceptColumn.HeaderText = "Cant. Iesita";
                CantitateReceptionataColumn.HeaderText = "Cant. Iesita";
                CantitateComandataColumn.HeaderText = "Cant. Intrata";
                Text = "Stocuri pe documente de intrare";
            }

        }

        private Boolean _loadForm = false;
        private void ComenziWizFrm_Load(object sender, EventArgs e)
        {
            
            _loadForm = true;
            SqlConnection con = DeschideConexiune(workConnString);
            this.tipDocumentTableAdapter.Connection = con;
            this.tipDocumentTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.TipDocument);
            this.valutaTableAdapter.Connection = con;
            this.faresSetupTableAdapter.Connection = con;
            this.faresSetupTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.FaresSetup,ModuleInfo.CodUnitate);            
            this.valutaTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.Valuta);

            this.unitatiMasuraTableAdapter.Connection = con;
            this.unitatiMasuraTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.UnitatiMasura);
            this.articoleTableAdapter.Connection = con;
            this.articoleTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.Articole);

            this.fares_viewComenziNeonorateTableAdapter.Connection = con;
            if (IDArticol == -1)
            {
                if (IDPartener == -1)
                    this.fares_viewComenziNeonorateTableAdapter.Fill(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate);
                else
                {
                    switch(IDTipDocumentSursa)
                    {
                        case 0:
                        case 15:
                            this.fares_viewComenziNeonorateTableAdapter.FillByPartener(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate, IDPartener,9);
                        break;
                        case 13:
                        case 14:
                        case 1:
                       
                            this.fares_viewComenziNeonorateTableAdapter.FillByPartener(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate, IDPartener,12);
                        break;

                    }
                }
            }
            else
                if (GestiuneSt0c=="ST")
                    this.fares_viewComenziNeonorateTableAdapter.FillByArticolStoc(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate, IDArticol);
                else
                {
                    switch (IDTipDocumentSursa)
                    {
                        case 0:
                        case 15:
                            this.fares_viewComenziNeonorateTableAdapter.FillByArticol(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate, IDArticol, 9);
                            break;
                        case 13:
                        case 14:
                        case 1:
                            this.fares_viewComenziNeonorateTableAdapter.FillByArticol(this.eSRPDataSetComenziNeonorate.fares_viewComenziNeonorate, ModuleInfo.CodUnitate, IDArticol, 12);
                            break;

                    }
                }

            this.documenteDetaliuLinkTableAdapter.Connection = con;
            this.documenteDetaliuLinkTableAdapter.FillByDest(this.eSRPDataSetComenziNeonorate.DocumenteDetaliuLink, IDDocDetaliuDest,GestiuneSt0c);
            InchideConexiune(con);
            
            Setup();
            AddButton("Salveaza", global::ERPStudio.Properties.Resources.delicious_48, SalveazaBifa, ToolStripItemAlignment.Right, false);

            
            //ArticoleColumn.Visible = WizardComenzi || (GestiuneSt0c=="ST");
            //CantitateReceptColumn.Visible = WizardComenzi || (GestiuneSt0c == "ST");
            ColumnValoareLei.Visible = WizardComenzi && !(GestiuneSt0c == "ST");
            ColumnValoareValuta.Visible = WizardComenzi && !(GestiuneSt0c == "ST");
            SelectColumn.Visible = WizardComenzi;
            valoareUnitaraColumn.Visible = (GestiuneSt0c == "ST" || GestiuneSt0c == "CM");
            IDTipDocumentColumn.Visible = (GestiuneSt0c == "ST" || GestiuneSt0c == "CM");

            if (WizardComenzi)
            {
                fares_viewComenziNeonorateBindingSource.Filter = "CantitateRamasa>0";
            }

            if(fares_viewComenziNeonorateBindingSource.Count!=0)
            {
                fares_viewComenziNeonorateBindingSource.Sort = "DataDoc";
                fares_viewComenziNeonorateBindingSource.MoveLast();
                DataSets.ESRPDataSetComenziNeonorate.fares_viewComenziNeonorateRow fr = ((DataRowView)fares_viewComenziNeonorateBindingSource.Current).Row
                    as DataSets.ESRPDataSetComenziNeonorate.fares_viewComenziNeonorateRow;
                if (fr != null)
                {
                    PanaLaDTP.Value = fr.DataDoc;
                }
                fares_viewComenziNeonorateBindingSource.MoveFirst();
                fr = ((DataRowView)fares_viewComenziNeonorateBindingSource.Current).Row
                    as DataSets.ESRPDataSetComenziNeonorate.fares_viewComenziNeonorateRow;
                if (fr != null)
                {
                    DeLaDTP.Value = fr.DataDoc;
                }
                
            }

            /// bifarea este ultima sectiune din functie
            /// 
            Decimal deDistribuit = CantitateIesita;
            foreach (DataGridViewRow dgr in comenziDGV.Rows)
            {
                if (dgr.Cells[IndexIDDocDetaliuDest].Value != DBNull.Value)
                    if (Convert.ToInt64(dgr.Cells[IndexIDDocDetaliuDest].Value) == IDDocDetaliuDest)
                        dgr.Cells[IndexSelect].Value = true;

                ///CantitateRamasaColumn
                /////CantitateReceptColumn
               
                if (dgr.Cells[CantitateIesitaColumn.Index].Value != DBNull.Value)
                {
                    dgr.Cells[CantitateReceptColumn.Index].Value = Convert.ToDecimal(dgr.Cells[CantitateIesitaColumn.Index].FormattedValue);
                    deDistribuit -= Convert.ToDecimal(dgr.Cells[CantitateReceptColumn.Index].Value);
                }
               
            }

            if (deDistribuit != Decimal.Zero)
            {
                foreach (DataGridViewRow dgr in comenziDGV.Rows)
                {
                    ///CantitateRamasaColumn
                    /////CantitateReceptColumn
                    if(deDistribuit<=Decimal.Zero)
                        break;

                    if (dgr.Cells[CantitateRamasaColumn.Index].Value != DBNull.Value && Convert.ToDecimal(dgr.Cells[CantitateRamasaColumn.Index].Value) != Decimal.Zero)
                    {
                        Decimal ram =Convert.ToDecimal(dgr.Cells[CantitateRamasaColumn.Index].Value);
                        dgr.Cells[CantitateReceptColumn.Index].Value = Convert.ToDecimal(dgr.Cells[CantitateIesitaColumn.Index].FormattedValue) + (deDistribuit>ram?ram:deDistribuit) ;
                        if(deDistribuit>ram)
                            deDistribuit -= ram;
                        else
                            deDistribuit = Decimal.Zero;
                    }
                }
            }

            

            _loadForm = false;
        }

        private void Setup()
        { 
            if(FaresBindingSource.Current==null)
                return;
            DataSets.ESRPDataSetComenziNeonorate.FaresSetupRow fsr = ((DataRowView)FaresBindingSource.Current).Row as DataSets.ESRPDataSetComenziNeonorate.FaresSetupRow;
            valoareUnitaraColumn.Visible = !fsr.ComandaFaraPreturi;
        }

        private void SalveazaBifa(object sender, EventArgs e)
        {
            comenziDGV.EndEdit();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void DeLaDTP_ValueChanged(object sender, EventArgs e)
        {
            if (_loadForm)
                return;
            DateTime dl = DeLaDTP.Value;
            DateTime pl = PanaLaDTP.Value;

            fares_viewComenziNeonorateBindingSource.Filter = "DataDoc >= '" + dl.Year.ToString() + "-" + dl.Month.ToString() + "-" + dl.Day.ToString() + "'  AND  DataDoc <='"
                + pl.Year.ToString() + "-" + pl.Month.ToString() + "-" + pl.Day.ToString() + "'" +
                (ToateComenzileCB.Checked?"": " AND CantitateRamasa>0");

        }

        private void comenziDGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow dgrv in comenziDGV.Rows)
            {
                dgrv.Cells[CantitateReceptColumn.Index].Value = dgrv.Cells[CantitateRamasaColumn.Index].Value;
            }
        }

       
    }
}
